﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using XRpgLibrary.TileEngine;
using XRpgLibrary.ScriptClasses;
using XRpgLibrary.ExtensionMethods;

namespace RpgLibrary.WorldClasses 
{
    public class MapData : IRpgDataObject
    {
        public string Name
        {
            get
            {
                return MapName;
            }
            set
            {
                MapName = value;
            }
        }
        public string MapName;
        public MapLayerData[] Layers;
        public TilesetData[] Tilesets;
        
        private MapData()
        {
        }
        
        public MapData(string mapName, List<TilesetData> tilesets, List<MapLayerData> layers)
        {
            MapName = mapName;
            Tilesets = tilesets.ToArray();
            Layers = layers.ToArray();
        }

        public List<Tileset> LoadTilesets(Game game)
        {
            List<Tileset> tileSets = new List<Tileset>();

            foreach (TilesetData data in this.Tilesets)
            {

                Texture2D texture = game.Content.Load<Texture2D>(data.TilesetImageName); 
                Texture2D passTexture = null;

                if(game.Content.ContentExists(data.TilesetPassabilityImageName))
                    passTexture = game.Content.Load<Texture2D>(data.TilesetPassabilityImageName);

                tileSets.Add(
                    new Tileset(
                        texture,
                        passTexture,
                        data.TilesWide,
                        data.TilesHigh,
                        data.TileWidthInPixels,
                        data.TileHeightInPixels,
                        data.AnimationScript));
            }

            return tileSets;
        }

        public List<MapLayer> LoadLayers(Game game, SpriteBatch spriteBatch, ScriptContext scriptContext, Camera camera)
        {
            List<MapLayer> layers = new List<MapLayer>();
            foreach (MapLayerData data in this.Layers)
                layers.Add(MapLayer.FromLayerData(data, spriteBatch, game.Content, scriptContext, camera));

            return layers;
        }
    }
}